home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 007 (1987-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 007 (1987-02-15)(Ossowski, Stefan)(DE)(PD).adf / C Compiler / memmgt.c < prev    next >
C/C++ Source or Header  |  1987-03-04  |  4KB  |  122 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *    all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. struct blk {
  26.         struct blk      *next;
  27.         char            m[1];           /* memory area */
  28.         };
  29.  
  30. static int      glbsize = 0,    /* size left in current global block */
  31.                 locsize = 0,    /* size left in current local block */
  32.                 glbindx = 0,    /* global index */
  33.                 locindx = 0;    /* local index */
  34.  
  35. static struct blk       *locblk = 0,    /* pointer to local block */
  36.                         *glbblk = 0;    /* pointer to global block */
  37.  
  38. char    *xalloc(siz)
  39. int     siz;
  40. {       struct blk      *bp;
  41.         char            *rv;
  42.     if( siz & 1 )        /* if odd size */
  43.         siz += 1;    /* make it even */
  44.         if( global_flag ) {
  45.                 if( glbsize >= siz ) {
  46.                         rv = &(glbblk->m[glbindx]);
  47.                         glbsize -= siz;
  48.                         glbindx += siz;
  49.                         return rv;
  50.                         }
  51.                 else    {
  52.                         bp = calloc(1,sizeof(struct blk) + 2047);
  53.             if( bp == NULL )
  54.             {
  55.                 printf(" not enough memory.\n");
  56.                 exit(1);
  57.             }
  58.                         bp->next = glbblk;
  59.                         glbblk = bp;
  60.                         glbsize = 2048 - siz;
  61.                         glbindx = siz;
  62.                         return glbblk->m;
  63.                         }
  64.                 }
  65.         else    {
  66.                 if( locsize >= siz ) {
  67.                         rv = &(locblk->m[locindx]);
  68.                         locsize -= siz;
  69.                         locindx += siz;
  70.                         return rv;
  71.                         }
  72.                 else    {
  73.                         bp = calloc(1,sizeof(struct blk) + 2047);
  74.             if( bp == NULL )
  75.             {
  76.                 printf(" not enough local memory.\n");
  77.                 exit(1);
  78.             }
  79.                         bp->next = locblk;
  80.                         locblk = bp;
  81.                         locsize = 2048 - siz;
  82.                         locindx = siz;
  83.                         return locblk->m;
  84.                         }
  85.                 }
  86. }
  87.  
  88. release_local()
  89. {       struct blk      *bp1, *bp2;
  90.         int             blkcnt;
  91.         blkcnt = 0;
  92.         bp1 = locblk;
  93.         while( bp1 != 0 ) {
  94.                 bp2 = bp1->next;
  95.                 free( bp1 );
  96.                 ++blkcnt;
  97.                 bp1 = bp2;
  98.                 }
  99.         locblk = 0;
  100.         locsize = 0;
  101.         lsyms.head = 0;
  102.         printf(" releasing %d bytes local tables.\n",blkcnt * 2048);
  103. }
  104.  
  105. release_global()
  106. {       struct blk      *bp1, *bp2;
  107.         int             blkcnt;
  108.         bp1 = glbblk;
  109.         blkcnt = 0;
  110.         while( bp1 != 0 ) {
  111.                 bp2 = bp1->next;
  112.                 free(bp1);
  113.                 ++blkcnt;
  114.                 bp1 = bp2;
  115.                 }
  116.         glbblk = 0;
  117.         glbsize = 0;
  118.         gsyms.head = 0;         /* clear global symbol table */
  119.         printf(" releasing %d bytes global tables.\n",blkcnt * 2048);
  120.         strtab = 0;             /* clear literal table */
  121. }
  122.